package com.share.module.sys.menu.web;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.stock.common.constant.GlobalConstant;
import com.share.module.sys.menu.service.MenuService;
import com.share.module.sys.dict.entity.Dict;
import com.share.module.sys.dict.service.DictService;
import com.share.module.sys.menu.entity.Menu;
import com.stock.common.response.ApiResult;
import com.stock.mybatisplus.utils.PageUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author lijy
 * @since 2023-04-17
 */
@Tag(name = "菜单管理", description = "菜单增删改查及图标管理接口")
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Resource
    private MenuService menuService;

    @Resource
    private DictService dictService;

    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "分页查询菜单列表")
    public ApiResult<PageUtils> page(@Parameter(description = "当前页", example = "1") @RequestParam Integer current,
                                     @Parameter(description = "每页个数", example = "10") @RequestParam Integer size,
                                     @Parameter(description = "菜单名称") @RequestParam(defaultValue = "") String name) {
        PageUtils pageUtils = menuService.page(current, size, name);
        return ApiResult.success(pageUtils);
    }

    @Operation(summary = "列表数据", description = "查询所有菜单列表")
    @GetMapping
    public ApiResult<List<Menu>> findAll(@Parameter(description = "菜单名称") @RequestParam(defaultValue = "") String name) {
        return ApiResult.success(menuService.findAll(name));
    }

    @Operation(summary = "保存或更新", description = "保存或更新菜单信息")
    @PostMapping
    public ApiResult<Boolean> save(@Parameter(description = "菜单实体") @RequestBody Menu menu) {
        return ApiResult.success(menuService.saveOrUpdate(menu));
    }

    @Operation(summary = "批量删除", description = "批量删除菜单")
    @PostMapping("/del/batch")
    public ApiResult<Boolean> batchDel(@Parameter(description = "菜单ID列表") @RequestBody List<Integer> ids) {
        return ApiResult.success(menuService.removeByIds(ids));
    }

    @Operation(summary = "删除", description = "根据id删除菜单")
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> delete(@Parameter(description = "菜单ID") @PathVariable Integer id) {
        return ApiResult.success(menuService.removeById(id));
    }

    @Operation(summary = "查找所有id", description = "查找所有菜单的ID列表")
    @GetMapping("/ids")
    public ApiResult<?> findAllIds() {
        return ApiResult.success(menuService.list().stream().map(Menu::getId));
    }

    @Operation(summary = "查找", description = "根据id查找菜单")
    @GetMapping("/{id}")
    public ApiResult<Menu> findOne(@Parameter(description = "菜单ID") @PathVariable Integer id) {
        return ApiResult.success(menuService.getById(id));
    }

    @Operation(summary = "icon列表", description = "获取所有图标列表")
    @GetMapping("/icons")
    public ApiResult<List<Dict>> getIcons() {
        return ApiResult.success(dictService.list(Wrappers.<Dict>lambdaQuery().eq(Dict::getType, GlobalConstant.DICT_TYPE_ICON)));
    }
}

